// src/router/index.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/home/index.vue';
import Mission from '../views/mission/index.vue';
import Login from '../views/login/index.vue';
import LoginByEmail from '../views/login/loginbyemail.vue';
import Reward from '../views/reward/index.vue';
import MissionDetail from '../views/mission/detail.vue';
import SuccessDetail from '../views/success/detail.vue';
import Success from '../views/success/index.vue';
import Us from '../views/us/index.vue';
import RegisterByPhone from '../views/login/registerbyphone.vue';
import RegisterByEmail from '../views/login/registerbyemail.vue';
import ForgetPswPhone from '../views/login/forgetpswphone.vue';
import ForgetPswEmail from '../views/login/forgetpswemail.vue';
import Resetpsw from '../views/login/resetpsw.vue';
import PersonCenter from '../views/person/index.vue';
import Invite from '../views/invite/index.vue';
import Authbind from '../views/authbind/authbind.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Main',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      tabIndex: '/home'
    }
  },
  {
    path: '/mission',
    name: 'Mission',
    component: Mission,
    meta: {
      tabIndex: '/mission'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/loginbyemail',
    name: 'LoginByEmail',
    component: LoginByEmail
  },
  {
    path: '/personcenter',
    name: 'PersonCenter',
    component: PersonCenter
  },
  {
    path: '/registerbyphone',
    name: 'RegisterByPhone',
    component: RegisterByPhone
  },
  {
    path: '/registerbyemail',
    name: 'RegisterByEmail',
    component: RegisterByEmail
  },
  {
    path: '/forgetpswphone',
    name: 'ForgetPswPhone',
    component: ForgetPswPhone
  },
  {
    path: '/forgetpswemail',
    name: 'ForgetPswEmail',
    component: ForgetPswEmail
  },
  {
    path: '/resetpsw',
    name: 'Resetpsw',
    component: Resetpsw
  },
  {
    path: '/reward',
    name: 'Reward',
    component: Reward,
    meta: {
      tabIndex: '/home'
    }
  },
  {
    path: '/missiondetail',
    name: 'MissionDetail',
    component: MissionDetail,
    meta: {
      tabIndex: '/mission'
    }
  },
  {
    path: '/successdetail',
    name: 'SuccessDetail',
    component: SuccessDetail,
    meta: {
      tabIndex: '/success'
    }
  },
  {
    path: '/success',
    name: 'Success',
    component: Success,
    meta: {
      tabIndex: '/success'
    }
  },
  {
    path: '/invite/:code',
    name: 'Invite',
    component: Invite,
  },
  {
    path: '/authbind',
    name: 'Authbind',
    component: Authbind,
  },
  {
    path: '/us',
    name: 'Us',
    component: Us,
    meta: {
      tabIndex: '/us'
    }
  }
];

// 解决重复导航报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') throw err;
  });
};
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') throw err;
  });
};
const router = new VueRouter({
  mode: 'hash', // 使用 HTML5 History 模式
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { x: 0, y: 0 } // Vue 2 使用 x/y 表示横/纵坐标
  }
});


export default router;